/**
 * Copyright (C) 2011-2012 Rustam Bogubaev
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 */
package com.google.code.myminimvc;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.el.ELResolver;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.jsp.JspApplicationContext;
import javax.servlet.jsp.JspFactory;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA.
 * User: rustam
 * Date: 1/12/11
 * Time: 11:30 PM
 */
public abstract class Application implements ServletContextListener {
    private final Logger logger = LoggerFactory.getLogger(Application.class);
    public static final String KEY_REQUEST_MODEL_HOLDER = (Application.class.getName() + "_MODEL_" + UUID.randomUUID()).toUpperCase();

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
            ServletContext servletContext = servletContextEvent.getServletContext();

            registerELResolver(servletContext, new ModelELResolver());
            setup();

            logger.info(getClass().getName() + " context initialized");
        } catch (Exception e) {
            logger.error("unable to initialize context due to: ", e);
        }
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        logger.info(getClass().getName() + " context destroyed");
    }

    protected abstract void setup();

    protected Configuration getConfiguration() {
        return Configuration.get();
    }

    private void registerELResolver(ServletContext servletContext, ELResolver elResolver) {
        try {
            if (servletContext == null) {
                logger.warn("provided servlet context instance is null, so el resolver registration is skipped");
            }

            if (elResolver == null) {
                logger.info("provided el resolver instance is null, so el resolver registration is skipped");
                return;
            }

            Class.forName("org.apache.jasper.compiler.JspRuntimeContext");
            JspFactory jspFactory = JspFactory.getDefaultFactory();

            if (jspFactory == null) {
                logger.info("unable to get jsp factory, null returned, so el resolver registration is skipped");
                logger.info("see: " +
                        "\n\thttp://stackoverflow.com/questions/2551517/gae-j-unable-to-register-a-custom-elresolver" +
                        "\n\thttp://code.google.com/p/googleappengine/issues/detail?id=1506" +
                        "\n\thttp://code.google.com/p/googleappengine/issues/detail?id=3034" +
                        "\n\tfor more details");
                return;
            }

            JspApplicationContext jspContext = jspFactory.getJspApplicationContext(servletContext);
            jspContext.addELResolver(elResolver);

            logger.info(elResolver.getClass().getName() + " registered");
        } catch (Exception e) {
            logger.error("unable to register el resolver due to: ", e);
        }
    }
}


















